#pragma once
#include <string>
using std::string;
template<int size>
class Bitset {
    int bits[size] = { 0 };
    int ones = 0;
    int zeros=size;
    bool reverser = false;

public:
    Bitset()
    {
    }
    ~Bitset() {
    }
    void fix(int idx) {
        int index = idx / 32;
        int bit= idx % 32;
        if (!reverser)
        {
            if ((bits[index] & (1 << bit)) == 0) {
                bits[index] |= (1 << bit);
                ++ones;
                zeros--;
            }
        }
        else {
            if (bits[index] & (1 << bit)) {
                bits[index] ^= (1 << bit);
                ++ones;
                zeros--;
            }
        }
    }

    void unfix(int idx) {
        int index = idx / 32;
        int bit = idx % 32;
        if (!reverser)
        {
            if ((bits[index] & (1 << bit)) != 0) {
                bits[index] ^= (1 << bit);
                --ones;
                zeros++;
            }
        }
        else {
            if ((bits[index] & (1 << bit))==0) {
                bits[index] |= (1 << bit);
                --ones;
                zeros++;
            }
        }
    }
    void flip() {
        reverser = !reverser;
        int tem = ones;
        ones = zeros;
        zeros = tem;
    }

    bool all() {
        return ones == size;
    }

    bool one() {
        return ones >0;
    }

    int count() {
        return ones;
    }

    string toString() {
        string ans;
        ans.resize(size);
        for (int i=0,k=0,number,status;i<size;++k) {
            number = bits[k];
            for (int j=0;j<32&&i<size;++j,++i) {
                status = (number >> j) & 1;
                status ^= reverser ? 1 : 0;
                ans[i] = status + '0';
            }
        }
        return ans;
    }
};
class Bitset1 {
    int* bits = nullptr;
    int ones = 0;
    int zeros;
    int size;
    bool reverser = false;

public:
    Bitset1(int siz) : size(siz), zeros(siz)
    {
        bits = new int[(siz + 31) / 32];
        int l = (siz + 31) / 32;
        memset(bits, 0, l*4);
    }
    ~Bitset1() {
        if (bits)
            delete[] bits;
    }
    void fix(int idx) {
        int index = idx / 32;
        int bit = idx % 32;
        if (!reverser)
        {
            if ((bits[index] & (1 << bit)) == 0) {
                bits[index] |= (1 << bit);
                ++ones;
                zeros--;
            }
        }
        else {
            if (bits[index] & (1 << bit)) {
                bits[index] ^= (1 << bit);
                ++ones;
                zeros--;
            }
        }
    }

    void unfix(int idx) {
        int index = idx / 32;
        int bit = idx % 32;
        if (!reverser)
        {
            if ((bits[index] & (1 << bit)) != 0) {
                bits[index] ^= (1 << bit);
                --ones;
                zeros++;
            }
        }
        else {
            if ((bits[index] & (1 << bit)) == 0) {
                bits[index] |= (1 << bit);
                --ones;
                zeros++;
            }
        }
    }
    void flip() {
        reverser = !reverser;
        int tem = ones;
        ones = zeros;
        zeros = tem;
    }

    bool all() {
        return ones == size;
    }

    bool one() {
        return ones > 0;
    }

    int count() {
        return ones;
    }

    string toString() {
        string ans;
        ans.resize(size);
        for (int i = 0, k = 0, number, status; i < size; ++k) {
            number = bits[k];
            for (int j = 0; j < 32 && i < size; ++j, ++i) {
                status = (number >> j) & 1;
                status ^= reverser ? 1 : 0;
                ans[i] = status + '0';
            }
        }
        return ans;
    }
};